<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2021/9/23
  Time: 15:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@ include file="header.jsp"%>
    <title>Title</title>
</head>
<body>

    <table class="layui-hide" id="test"  lay-filter="layFilter"></table>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="insert">添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="deleteAll">批量删除</button>
        </div>
    </script>

    <script>
        layui.use('table', function(){
            var table = layui.table;

            table.render({
                elem: '#test'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,url:'${path}/user/selectByPage'
                ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                ,cols: [[
                     {type: 'checkbox'}
                    ,{field:'id', title: 'ID', sort: true}
                    ,{field:'name', title: '用户名'}
                    ,{field:'age', title: '年龄', sort: true}
                    ,{field:'password', title: '密码'}
                    ,{field:'level', title: '级别'}
                    ,{toolbar: '#barDemo'}
                ]]
                ,page: true
                ,id: 'tableId'
            });

            //头工具栏事件
            table.on('toolbar(layFilter)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'insert':

                        break;
                    case 'deleteAll':
                        var data = checkStatus.data;
                        var ids = []; // [34, 5]
                        $(data).each(function() {
                            // this
                            ids.push(this.id);
                        });
                        ids = ids.join(','); // '34,5'
                        layer.confirm('真的删除行么', function(index){
                            $.post(
                                '${path}/user/deleteAll',
                                {'ids' : ids},
                                function(jsonResult) {
                                    if(jsonResult.ok) {
                                        mylayer.okMsg(jsonResult.msg);
                                    }
                                    // 删除完了之后刷新一下table页面
                                    table.reload('tableId');
                                },
                                'json'
                            );
                        });
                        break;
                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听工具条
            table.on('tool(layFilter)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        $.post(
                            '${path}/user/deleteById',
                            {'id' : data.id},
                            function(jsonResult) {
                                // isOk()  getOk()  getCode
                                if(jsonResult.ok) {
                                    mylayer.okMsg(jsonResult.msg);
                                }
                                // 删除完了之后刷新一下table页面
                                table.reload('tableId');
                            },
                            'json'
                        );
                        layer.close(index);
                    });
                } else if(obj.event === 'update'){
                    layer.alert('编辑行：<br>'+ JSON.stringify(data))
                }
            });
        });
    </script>

</body>
</html>
